//
//  ViewController.m
//  11-QQ聊天
//
//  Created by wangneihan on 15/12/21.
//  Copyright © 2015年 wangneihan. All rights reserved.
//

#import "ViewController.h"
#import "Message.h"
#import "MessageFrame.h"
#import "MessageCell.h"

@interface ViewController () <UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic , strong) NSMutableArray *messageFrame;

@property (weak, nonatomic) IBOutlet UITextField *msgTextFiled;

@end

@implementation ViewController

-(NSArray *)messageFrame
{
    if (_messageFrame == nil) {
        NSArray *message = [Message message];
        NSMutableArray *tempArray = [NSMutableArray array];
        for (Message *msg in message) {
            MessageFrame *messageframe = [[MessageFrame alloc]init];
            messageframe.message = msg;
            [tempArray addObject:messageframe];
        }
        _messageFrame = tempArray;
    }
    return _messageFrame;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.backgroundColor = [UIColor lightGrayColor];
    //去掉间隔线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.allowsSelection = NO;
    self.tableView.backgroundColor = [UIColor colorWithRed:240.00/255.00 green:240.00/255.00 blue:240.00/255.00 alpha:1];
    
    self.msgTextFiled.delegate = self;
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.messageFrame.count - 1 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
    
    //1. 通过通知中心，订阅键盘代理
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

-(void)sendMsg:(NSString *)msgStr msgType:(MessageType)type
{
//    MessageFrame *msgFrame = [[MessageFrame alloc]init];
//    msgFrame.message = msg;
//    [self.messageFrame addObject:msg];
    Message *msg = [[Message alloc]init];
    
    msg.type = type;
    msg.text = msgStr;
    
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"HH:mm";
    
    msg.time = [formatter stringFromDate:date];
    
    //上一个消息
    Message *preMessage = [[self.messageFrame lastObject] message];
    if ([msg.time isEqualToString:preMessage.time]) {
        msg.hiddenTime = YES;
    }
    
    MessageFrame *msgFrame = [[MessageFrame alloc] init];
    msgFrame.message = msg;
    //
    [self.messageFrame addObject:msgFrame];
    
    [self.tableView reloadData];
    
    NSIndexPath *path = [NSIndexPath indexPathForRow:self.messageFrame.count - 1 inSection:0];
    [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionBottom animated:YES];

    
}

#pragma -mark 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.messageFrame.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MessageCell *cell = [MessageCell initWithTableView:tableView];
    cell.messageFrame = self.messageFrame[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageFrame *tempFrame = self.messageFrame[indexPath.row];
    return tempFrame.rowHight;
}

#pragma mark 文本框监听方法
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    NSLog(@"%s",__func__);
    return  YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self sendMsg:textField.text msgType:MessageTypeSelf];
    
    self.msgTextFiled.text = nil;
    
    [self.view endEditing:YES];
    
    return  YES;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

-(void)keyboardWillChangeFrame:(NSNotification *)noti
{
    CGRect endFrame = [noti.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    CGFloat moveFrame = endFrame.origin.y -  self.view.frame.size.height;
    CGFloat duration = [noti.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
    [UIView animateWithDuration:duration animations:^{
        self.view.transform = CGAffineTransformMakeTranslation(0, moveFrame);
    }];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



@end
